<?php
require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'check_user_login.php';
?>
<!DOCTYPE html>
<html>
<head id="Head1">
	<title>
		Categories - Tesdo Admin
	</title>
	<?php
	require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'head.php';
	require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'header.php';
	require_once DOCUMENT_ROOT . DIR_SHARED_DAO . 'dao_category.php';
	require_once DOCUMENT_ROOT . DIR_SHARED_DAO . 'dao_journal.php';
	require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_pager.php';
	?>
	<script type="text/javascript">
		function delete_by_id(id) {
			if (confirm('Are you sure?')) {
				set_value('delete_id', id);
				submit_form('form_category_delete');
			} else {
				return false;
			}
		}
		function add_new_category() {
			$('#cphAdmin_categoryare').dialog({modal:true, width:"500px"});
		}

		$(document).ready(function() {
			$("#add_new").on('click', function(e) {
				add_new_category();
			});
			<?php 
			if (!empty($_GET["edit_id"])) {
				// echo "$('#cphAdmin_categoryare').show();";
				echo "$('#cphAdmin_categoryare').dialog({modal:true, width:'500px'});";
			}
			?>
		});
	</script>
</head>
<body>
	<!-- Begin PHP Scripts -->
	<?php

	$category_access = new dao_category();
	$journal_access = new dao_journal();
	$lib_pager = new lib_pager();

	$categories = $category_access->get_all();

	$category_id = 0;
	$btn_value = "Add new";

        //Cancel
	if (!empty($_POST["btnCancel"])) {
		?>
		<script type="text/javascript">
			window.location = <?php echo LINK_ROOT . DIR_ADMIN . 'categories.php'; ?>;
		</script>
		<?php
	}

        //Add
	if (!empty($_POST["btnAdd"])) {
		$category_id = $_POST["category_id"];
		$title = $_POST["txt_title"];

		if ($category_access->save($category_id, $title) == TRUE) {
			?>
			<script type="text/javascript">
				alert('<?php if ($category_id==0) { echo "Added"; } else { echo "Updated"; }?> category successfully!');
				window.location = 'categories.php';
			</script> 
			<?php } else { ?>
			<script type="text/javascript">
				alert('Failed to <?php if ($category_id==0) { echo "add"; } else { echo "update"; }?> category!');
			</script> 
			<?php
		}
	}

        //Edit
	if (!empty($_GET["edit_id"])) {
		$btn_value = "Edit";
		$category_id = $_GET["edit_id"];
		$category_old = $category_access->get_by_id($category_id);
	}


        //Delete
	if (!empty($_POST["delete_id"])) {
		$category_id = $_POST["delete_id"];
		if ($category_access->delete($category_id)) {
			?>
			<script type="text/javascript">
				alert('Delete category successfully!');
				window.location = 'categories.php';
			</script> 
			<?php } else { ?>
			<script type="text/javascript">
				alert('Failed to delete category!');
			</script> 
			<?php

		}
	}

	?>
	<!-- End Of PHP Scripts  -->

	<!-- Begin Form -->
	<div>
		<div class="content-box-outer">
			<div class="content-box-right">
			</div>
			<div class="content-box-left">
				<h1>Categories<a id="add_new" href="#" class="addNew cboxElement">Add new category</a></h1>
				<div id="cphAdmin_categoryare" style="display: none;">
					<form action="categories.php" method="POST" id="form_category_save" >
						<div id="frmAddNew" class="overlaypanel">
							<h2><span id="cphAdmin_lblCatTitle"><?php echo $btn_value; ?> category</span></h2>
							<ul class="fl">
								<input type="hidden" name="category_id" value="<?php echo $category_id; ?>" />
								<li>
									<label for="cphAdmin_txtNewCategory" id="cphAdmin_lblNewCategory">Title</label><br>
									<input name="txt_title" value="<?php if(!empty($category_old["Title"])) {echo $category_old["Title"];} ?>" type="text" maxlength="50" id="cphAdmin_txtNewCategory" style="width:200px;">
									<span id="cphAdmin_valExist" style="display:none;">Alread esxit</span>
									<span id="cphAdmin_RequiredFieldValidator1" style="display:none;">enter valid name</span>
								</li>
							</ul>
							<input type="submit" name="btnAdd" value="<?php echo $btn_value; ?>" onclick="colorboxDialogSubmitClicked('new', 'frmAddNew'); WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions( & quot; ctl00$cphAdmin$btnAdd & quot; , & quot; & quot; , true, & quot; new & quot; , & quot; & quot; , false, false))" id="cphAdmin_btnAdd" class="btn primary">
							or <a href="<?php echo LINK_ROOT . DIR_ADMIN . 'categories.php'; ?>" onclick="closeOverlay();">Cancel</a>
						</div>
					</form>
				</div>
				<form action="categories.php" method="POST" id="form_category_delete" >
					<input type="hidden" name="delete_id" value="0" />
				</form>
				<div class="MessageArea">
					<span id="cphAdmin_lblMsg"></span>
				</div>
				<div>

					<div>
						<table cellspacing="0" cellpadding="4" rules="cols" id="cphAdmin_gvCategories" style="color:Black;background-color:White;border-color:#DEDFDE;border-width:1px;border-style:None;width:988px;border-collapse:collapse;">
							<tbody><tr style="color:White;background-color:#6B696B;font-weight:bold;">
								<th scope="col">Category ID</th><th scope="col">Categoriy Title</th><th scope="col">Actions</th>
							</tr>
							<?php

							$current_page = 1;
							$page_size = 10;
							if (!empty($_GET['p'])) {
								$current_page = $_GET['p'];
							}

							$list_total = $category_access->get_all();
							$total_page = $lib_pager->get_total_page($list_total, $page_size);
							$current_page_list = $lib_pager->get_current_page_list($page_size, $current_page, $list_total);

							$count = 0;

							foreach ($current_page_list as $i => $item) {
								if ($count % 2 == 0) {
									?>
									<tr style="background-color:#F7F7DE;">
										<?php } else { ?>
										<tr style="background-color:White;">
											<?php } ?>
											<td style="width:350px;">
												<?php echo $item['CategoryID']; ?>
											</td><td><?php echo $item['Title']; ?></td><td align="center">
											<a href="categories.php?edit_id=<?php echo $item['CategoryID'] ?>">
												<input type="image" name="ImageButtonEdit" id="cphAdmin_gvCategories_ImageButtonEdit_0" title="Edit" src="<?php echo LINK_ROOT.DIR_ADMIN_LAYOUT_IMAGE; ?>/edit.png" style="width:15px;">
											</a>
											<input type="image" name="ImageButtonDelete" id="cphAdmin_gvCategories_ImageButtonDelete_0" title="Delete" src="<?php echo LINK_ROOT.DIR_ADMIN_LAYOUT_IMAGE; ?>/delete.png" onclick="return delete_by_id(<?php echo $item['CategoryID'] ?>);" style="width:15px;">
										</td>
										<?php
										$count++;
									}
									?>
								</tr><tr align="right" style="color:Black;background-color:#F7F7DE;">
								<td colspan="3">
									<table>
										<tbody>
											<tr>
												<?php for ($a = 1; $a <= $total_page; $a++) { ?>
												<td>
													<span>
														<a href="categories.php?p=<?php echo $a; ?>" <?php if ($current_page != $a) echo 'style="color:Black;"'; ?>><?php echo $a; ?></a>
													</span>
												</td>
												<?php } ?>
											</tr>
										</tbody>
									</table>
								</td>
							</tr>
						</tbody>
					</table>
				</div>
			</div>

		</div>
	</div>
</div>

<!-- End Form -->
<!-- Begin Footer -->

<?php
require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'footer.php';
?>

<!-- End Footer -->
</body>
</html>